package xcmg.device.web.company;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyDealerService;
import xcmg.device.service.company.DealerVehicleService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.company.CompanyDealerVO;
import xcmg.device.service.vo.company.DealerVehicleVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Created by wrg on 2019/4/19.
 */
@RestController
@RequestMapping("/dealerVehicle")
@Api("客户车辆")
public class DealerVehicleController extends BasicController {
    @Autowired
    private DealerVehicleService dealerVehicleService;
    @Autowired
    private CompanyDealerService companyDealerService;

    @PostMapping(value = "/insert")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "新增客户车辆")
    public ActionResult insert(@RequestBody DealerVehicleVO dealerVehicleVO)throws Exception{
        return actionResult(dealerVehicleService.insert(dealerVehicleVO,getUserId(),getOrgId()));
    }

    @PostMapping(value = "/update")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "修改客户车辆")
    public ActionResult update(@RequestBody DealerVehicleVO dealerVehicleVO){
        if(StringUtils.isBlank(dealerVehicleVO.getId())){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(dealerVehicleService.update(dealerVehicleVO,getUserId(),getUserName()));
    }

    @PostMapping(value = "/disable")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "删除客户分级")
    public ActionResult disable(@RequestBody DealerVehicleVO dealerVehicleVO){
        if(StringUtils.isBlank(dealerVehicleVO.getId())){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(dealerVehicleService.disable(dealerVehicleVO,getUserId()));
    }

    @PostMapping(value = "/list")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取客户车辆列表")
    public ActionResult list(@RequestBody SearchCommonVO<DealerVehicleVO> condition) throws Exception{
        DealerVehicleVO dealerVehicleVO = condition.getFilters();
        if (null == dealerVehicleVO){
            dealerVehicleVO = new DealerVehicleVO();
        }
        dealerVehicleVO.setOrgId(getOrgId());
        condition.setFilters(dealerVehicleVO);
        PageCommonVO pageCommonVO = dealerVehicleService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @PostMapping(value = "/notDeactivatedList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取客户车辆列表")
    public ActionResult notDeactivatedList(@RequestBody SearchCommonVO<DealerVehicleVO> condition) throws Exception{
        DealerVehicleVO dealerVehicleVO = condition.getFilters();
        if (null == dealerVehicleVO){
            dealerVehicleVO = new DealerVehicleVO();
        }
        dealerVehicleVO.setOrgId(getOrgId());
        condition.setFilters(dealerVehicleVO);
        PageCommonVO pageCommonVO = dealerVehicleService.notDeactivatedList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    /**
     * @Des 销售单（进出口）获取客户列表
     * 查询条件 companyName
     * @Date 2021/3/18 9:18
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/jinchukouNotDeactivatedList")
    public ActionResult jinchukouNotDeactivatedList(@RequestBody SearchCommonVO<CompanyDealerVO> condition) throws Exception {

        CompanyDealerVO companyDealerVO=condition.getFilters();
        if(companyDealerVO==null){
            companyDealerVO=new CompanyDealerVO();
        }
        if(companyDealerVO.getOrgId()==null){
            companyDealerVO.setOrgId(getOrgId());
        }
        condition.setFilters(companyDealerVO);
        PageCommonVO pageCommonVO=dealerVehicleService.saleBillDealerList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @GetMapping(value = "/detail")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取详情")
    public ActionResult detail(@RequestParam(value = "id")  String id){
        if(StringUtils.isBlank(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(dealerVehicleService.getDetail(id));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部车辆信息")
    @FastMappingInfo(needLogin = true)
    public void exportAll(DealerVehicleVO dealerVehicleVO) throws Exception
    {
        if (null == dealerVehicleVO){
            dealerVehicleVO = new DealerVehicleVO();
        }
        dealerVehicleVO.setOrgId(getOrgId());
        dealerVehicleService.exportAll(dealerVehicleVO);
    }

    @ResponseBody
    @PostMapping(value = "/importVehicleExcel")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出客户车辆信息")
    public ActionResult importVehicleExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            try {
                result= dealerVehicleService.importVehicleExcel(file, getOrgId(),getUserId());
            } catch (Exception e) {
                return actionResult(ErrorCode.Failure, e.getMessage());
            }

        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/downloadImportExcel",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导入模板下载")
    public void downloadImportExcel() throws Exception {
        dealerVehicleService.downloadImportExcel();
    }

    @PostMapping(value = "/getVehicleList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取客户车辆列表")
    public ActionResult getVehicleList(@RequestBody SearchCommonVO<DealerVehicleVO> condition) throws Exception{
        DealerVehicleVO dealerVehicleVO = condition.getFilters();
        if (null == dealerVehicleVO){
            dealerVehicleVO = new DealerVehicleVO();
        }
        dealerVehicleVO.setOrgId(getOrgId());
        condition.setFilters(dealerVehicleVO);
        PageCommonVO pageCommonVO = dealerVehicleService.getVehicleList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/getVehicleNoList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取整机编号列表")
    public ActionResult getVehicleNoList(@RequestParam("clientName") String clientName) throws Exception{
        if (StringUtils.isBlank(clientName)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(dealerVehicleService.getVehicleNoList(clientName,getOrgId()));
    }

    @GetMapping(value = "/getPersonVehicleInfo")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取个人整机额度信息")
    public ActionResult getPersonVehicleInfo(@RequestParam("clientName") String clientName,@RequestParam("carNo") String carNo) throws Exception{
        if (StringUtils.isBlank(clientName)|| StringUtils.isBlank(carNo)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(dealerVehicleService.getPersonVehicleInfo(clientName,getOrgId(),carNo));
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult delete(@RequestBody String[] ids){
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            ErrorCode ret=dealerVehicleService.delete(Arrays.asList(ids));
            return actionResult(ret);
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }
}
